<script setup lang="ts">
import { useK8SApi } from "@/views/domain/kubeHud/api/k8s/k8s";
import { type DaemonSet } from "@/views/domain/kubeHud/api/k8s/types";
import { onMounted, reactive, ref } from "vue";
import NamespaceSelector from "../namespace/widgets/NamespaceSelector.vue";
import RouterLinkBtn from "@/views/widgets/RouterLinkBtn/index.vue";
import { KubeHudRouterPath } from "@/views/domain/kubeHud/router";
const { daemonSetRepo } = useK8SApi();

const daemonSetList = ref<DaemonSet[]>();
const loading = ref(false);
const query = reactive<{
  namespace?: string;
}>({ namespace: "default" });

const reloadDaemonSetList = async () => {
  loading.value = true;

  const { data, ok } = await daemonSetRepo.getDaemonSet({
    namespace: query.namespace
  });

  if (ok) {
    daemonSetList.value = data.items;
  }

  loading.value = false;
};

onMounted(() => {
  reloadDaemonSetList();
});
</script>

<template>
  <div>
    <el-card>
      <el-form v-model="query" inline label-position="top">
        <el-form-item label="Namespace" prop="namespace">
          <NamespaceSelector
            v-model="query.namespace"
            @change="reloadDaemonSetList"
          />
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="daemonSetList" style="width: 100%">
        <el-table-column prop="metadata.name" label="Name">
          <template #default="{ row }">
            <RouterLinkBtn
              :to="KubeHudRouterPath.DaemonSetDetails"
              :label="row.metadata?.name"
              :query="{ namespace: query.namespace, name: row.metadata?.name }"
            />
          </template>
        </el-table-column>
        <el-table-column prop="metadata.namespace" label="Namespace" />
        <el-table-column prop="spec.selector.matchLabels" label="Selector" />
      </el-table>
    </el-card>
  </div>
</template>
<style lang="scss" scoped></style>
